﻿public class Solution {
    public int Jump(int[] nums)
    {
        if (nums.Length == 1) return 0;
        // 记录最少跳跃次数
        int jump = 0;
        
        // 当前可到达的最远距离
        int curDistance = 0;
        
        // 下一步可到达的最远距离
        int nextDistance = 0;

        for (int i = 0; i < nums.Length; i++)
        {
            nextDistance = int.Max(i + nums[i], nextDistance);
            if (i >= curDistance)
            {
                jump++;
                curDistance = nextDistance;
            }

            if (curDistance >= nums.Length - 1) break;
        }

        return jump;
    }

    public static void Main(string[] args)
    {
        int[] nums = [2, 3, 0, 1, 4];
        Solution solution = new Solution();
        int res = solution.Jump(nums);
        Console.WriteLine(res);
    }
}